import os
import re
import csv
import logging
from datetime import datetime

import config


def find_all_log_files(log_path, suffix):
    log_files = []
    for root, _, files in os.walk(log_path):
        for file in files:
            if file.endswith(suffix):
                full_path = os.path.join(root, file)
                log_files.append(full_path)
                # print(f"找到日志文件：{full_path}")
    return log_files


def extract_data(log_path, suffix, pattern, Encoding):
    """
    从日志文件中提取数据
    :param log_path: 日志文件路径
    :param suffix: 日志文件后缀
    :param pattern: 查找匹配模式
    :param Encoding: 日志文件编码
    :return: None
    """
    # 添加日志记录
    logging.basicConfig(level=logging.INFO, format='%(message)s')
    logging.info("开始查找日志文件")
    logging.info("日志文件路径：{}".format(log_path))
    logging.info("日志文件后缀：{}".format(suffix))
    logging.info("日志文件编码：{}".format(Encoding))
    logging.info("查找匹配模式：{}".format(pattern))

    data_counts = 0  # 初始化 data_counts

    all_logs_path = find_all_log_files(log_path, suffix)

    # 检查是否找到日志文件
    if not all_logs_path:
        logging.warning("未找到任何日志文件。")
        return -1

    extracted_data = []  # 初始化 extracted_data

    for full_path in all_logs_path:
        with open(full_path, 'r', encoding=Encoding, errors='replace') as file:
            for line_num, line in enumerate(file, start=1):
                try:
                    match = re.search(pattern, line)
                    if match:
                        data_value = match.group(1)
                        extracted_data.append([full_path, data_value])
                        data_counts += 1
                except UnicodeDecodeError:
                    logging.warning(f"文件 {full_path} 第 {line_num} 行解码失败，跳过该行。")
                    continue

    # 将数据保存到CSV文件中
    if extracted_data:
        # 生成时间戳
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        csv_file_name = f"extracted_data_{timestamp}.csv"
        csv_file_path = os.path.join(log_path, csv_file_name)

        # 写入文件
        with open(csv_file_path, 'w', newline='', encoding=Encoding) as csvfile:
            csv_writer = csv.writer(csvfile)
            csv_writer.writerow(['文件路径', '匹配值'])
            csv_writer.writerows(extracted_data)

    logging.info("数据提取完成")

    return data_counts


if __name__ == "__main__":
    import sys
    if len(sys.argv) != 2:
        print("Usage: python script_name.py log_path")
    else:
        log_path = sys.argv[1]
        extract_data(log_path, config.suffix, config.pattern, config.encoding)
